/**
 * 
 */
package hichecker.ui.components.custom_table.table_cell_renderer;

import hichecker.controller.Config;
import hichecker.ui.custom_views.conflict_handling.ConflictImportedTableModel;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.event.TableModelEvent;

/**
 *
 */
public class ConflictTypeTableCellRenderer extends CustomTableCellRenderer {

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
			boolean hasFocus, int tableRow, int tableColumn) {
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, tableRow, tableColumn);
		
		// It's the wrong model
		if (!(table.getModel() instanceof ConflictImportedTableModel)) return this;
		
		// Get the model
		ConflictImportedTableModel model = (ConflictImportedTableModel) table.getModel();
		int row = table.convertRowIndexToModel(tableRow);
		int column = table.convertColumnIndexToModel(tableColumn);
		
		// Change the color depending on the type of conflict of the cell
		Color bg = table.getBackground();
		switch (model.getCellConflictType(row, column)) {
			case NEW:
				bg = Config.COLOR_NEW_DATA;
				break;
			case OVERWRITING:
				bg = Config.COLOR_CONFLICT_DATA;
				break;
			case NONE:
				// TODO think about something more beautiful...
				boolean hasChanged = model.cellsHaveChanged(row,
						TableModelEvent.ALL_COLUMNS);
				bg = hasChanged ? (isSelected ? new Color(124, 170, 207) : new Color(253, 237, 162))
						: (isSelected ? table.getSelectionBackground() : table.getBackground());
				setBackground(bg);
				break;
		}
		setBackground(bg);
		
		return this;
	}
	
}
